package com.tg.app.util;

import android.graphics.Bitmap;
import android.util.Log;

import com.tg.app.bean.LocalThumbnail;
import com.tg.app.bean.LocalThumbnail_;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.imagecache.ImageCache;

import io.objectbox.Box;


public class LocalThumbnailUtils {

    private static final String TAG = "LocalThumbnailUtilsTAG";

    private static volatile LocalThumbnailUtils localThumbnailUtils;
    private Box<LocalThumbnail> box;
    private ImageCache imageCache;
    public synchronized static LocalThumbnailUtils getInstance() {
        if (localThumbnailUtils == null) {
            localThumbnailUtils = new LocalThumbnailUtils();
        }
        return localThumbnailUtils;
    }

    private LocalThumbnailUtils() {
        box = ObjectBoxUtil.getLocalThumbnail();
        imageCache = new ImageCache(TGApplicationBase.getApplicationContext());

    }


    public Bitmap getBitmap(String fileName){
        LocalThumbnail thumbnail = LocalThumbnailUtils.getInstance().getLocalThumbnail(fileName);
        if (thumbnail != null) {
            return thumbnail.bitmap;
        }
        return  imageCache.getBitmapFromDiskCache(fileName);
    }

    public LocalThumbnail getLocalThumbnail(String uuid) {
        if (box != null) {
            LocalThumbnail localThumbnail = box.query().equal(LocalThumbnail_.uuid, uuid).build().findFirst();
            if (localThumbnail!=null){
                localThumbnail.bitmap = imageCache.getBitmapFromDiskCache(uuid);
                if (localThumbnail.bitmap != null && localThumbnail.bitmap.isRecycled()) {
                    return null;
                }
            }
            return localThumbnail;
        }
        return null;
    }

    public LocalThumbnail saveLocalThumbnailWithCopy(Bitmap bitmap, String uuid) {
        if (bitmap == null || uuid == null) {
            return null;
        }

        try {
            Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), bitmap.isMutable());
            if (newBitmap != null) {
                return saveLocalThumbnail(newBitmap, uuid);
            }
        } catch (Throwable ex) {
            TGLog.e(TAG, "saveLocalThumbnailWithCopy: error  =  " + ex);
            return null;
        }
        return null;
    }

    public LocalThumbnail saveLocalThumbnail(Bitmap bitmap, String uuid) {
        imageCache.addBitmapToCache(uuid,bitmap);
        if (box != null) {
            LocalThumbnail local = box.query().equal(LocalThumbnail_.uuid, uuid).build().findFirst();
            if (local == null) {
                local = new LocalThumbnail();
                local.uuid = uuid;
            }
            local.updatedAt = System.currentTimeMillis() / 1000;
            if (imageCache.isThisBitmapCached(uuid)){
                box.put(local);
                return local;
            }
        }
        return null;
    }

    public void deleteBitmap(String uuid) {
        if (box != null) {
            LocalThumbnail local = box.query().equal(LocalThumbnail_.uuid, uuid).build().findFirst();
            if (local != null) {
                imageCache.removeBitmapCache(uuid);
                box.remove(local);
            }
        }
    }

}
